package cn.edu.gcu.giftems.action;

import cn.edu.gcu.giftems.pojos.Order;
import cn.edu.gcu.giftems.pojos.User;
import cn.edu.gcu.giftems.service.OrderService;
import cn.edu.gcu.giftems.service.UserService;
import cn.edu.gcu.giftems.utils.PageUtils;
import cn.edu.gcu.giftems.utils.export.OrderExportUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;
import java.util.List;

/**
 * TODO: Description here...
 *
 * @author ZhengHQ
 * @create 2018-09-25
 * @see
 */
public class OrderAction extends ActionSupport implements ModelDriven<Order> {

    private OrderService orderService =
            (OrderService) new ClassPathXmlApplicationContext("applicationContext.xml")
                    .getBean("orderService");
    private UserService userService =
            (UserService) new ClassPathXmlApplicationContext("applicationContext.xml")
                    .getBean("userService");
    private Order order = new Order();
    private List<User> list;

    @Override
    public Order getModel() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public List<User> getList() {
        return list;
    }

    public void setList(List<User> list) {
        this.list = list;
    }

    /**
     * 分页查询订单的执行方法
     */
    private Integer currPage = 1;
    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }
    public String findByPage(){
        PageUtils<Order> pageUtils = orderService.findByPage(currPage);
        // ActionContext.getContext().getValueStack().push(pageUtils);
        pageUtils.setCurrPage(currPage);
        ActionContext.getContext().put("pageUtils", pageUtils);
        list = userService.findAll();
        return "success";
    }

    /**
     * 添加订单的执行方法
     */
    public String save(){
        orderService.save(order);
        return this.findByPage();
    }

    /**
     * 更新订单信息的执行方法
     */
    public String update(){
        if (orderService.findById(order.getId()) != null)
            orderService.update(order);
        return this.findByPage();
    }
    /**
     * 根据订单id删除订单
     */
    public String delete(){
        order = orderService.findById(order.getId());
        if (order != null)
            orderService.delete(order);
        return this.findByPage();
    }

    public String edit(){
        if (order.getId() > 0) {
            Order.Status raleaseStatus = order.getStatus();
            order = orderService.findById(order.getId());
            order.setStatus(raleaseStatus);
            orderService.update(order);
        }
        return this.findByPage();
    }

    public void export()
    {
        OrderExportUtils Export=new OrderExportUtils();
        try {
            Export.export(orderService);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("导出数据有误");
        }
    }

    public String findByParameter() {
        PageUtils<Order> pageUtils = null;
        String filter_name = ActionContext.getContext().getParameters().get("filter-name").toString();
        if (ActionContext.getContext().getParameters().get("begin-time").toString().equals("")) {
            String simple_filter_value;
            if (ActionContext.getContext().getParameters().get("simple-filter-value2").toString().equals("")) {
                simple_filter_value = ActionContext.getContext().getParameters().get("simple-filter-value").toString();
            } else {
                simple_filter_value = ActionContext.getContext().getParameters().get("simple-filter-value2").toString();
            }
            pageUtils = orderService.findByParameter(currPage, filter_name, simple_filter_value);
        }
        else {
            String begin_time = ActionContext.getContext().getParameters().get("begin-time").toString();
            String finish_time = ActionContext.getContext().getParameters().get("finish-time").toString();
            pageUtils = orderService.findByTime(currPage, begin_time, finish_time);
        }
        ActionContext.getContext().put("pageUtils", pageUtils);
        return "success";
    }
}
